//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "tmf\fn_execS.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

if ((isNil QGVAR(INITIALIZED)) || {!GVAR(INITIALIZED)}) exitWith {
	// Case when the framework is not initialized yet
	DEBUG_1("Delaying execution");
	_this spawn {
		waituntil{GVAR(INITIALIZED) && (time > 6)};
		_this call FFUNC(execS);
	};
};

if (!isServer) then {
	DEBUG("Remote call");
	[QGVAR(s_execute),_this] call FFUNC(callevent);
} else {
	private ["_params","_script","_i","_is_fnc","_hc_id","_fnc"];
	_params = [];
	_script = "";
	if (IS_STRING(_this)) then {
		_script = _this;
	} else {
		if (IS_ARRAY(_this) && {count _this > 0}) then {
			_script = _this select 0;
			_params = _this select 1;
		};
	};
	if (_script != "") then {
		_is_fnc = !(isNil _script);
		DEBUG_1("Server Execute",_is_fnc);
		if (_is_fnc) then {
			_fnc = (missionNameSpace getVariable [_script,""]);
			if (IS_STRING(_fnc)) then {
				_fnc = call compile _script;
			};
			_params spawn _fnc;
		} else {
			_params execVM _script;
		};
	};
};
TRACE("End");
TMFPROFILERSTOP;